/*
 * @Description: 我的收藏Hook
 * @Author: zhaoxiaoyang & seadon
 * @LastEditors: seadon
 * @Date: 2021-07-19 21:33:05
 * @LastEditTime: 2021-07-21 14:41:05
 */
import { ref } from 'vue'
import { apiInit } from '@/services'
import { useLoading } from '@/hooks/useLoading'
import store from '@/store/index'
import { isMoreTabs } from '@/utils/common'
export default function useFavorite() {
	const api = apiInit()
	const favoriteList = ref({})
	const defaultLoading = ref({ loading: false })
	const { getLoading, setLoading } = useLoading(defaultLoading)

	const fetchFavoriteList = async () => {
		setLoading(true)
		const res = await api.default.ListCollectUserPrivateV2POST({
			query: {
				enableMultipleTab: isMoreTabs(),
			},
		})
		setLoading(false)
		if (res.code === '000000') {
			const data = res.data?.map((item: any) => {
				return {
					name: item.resource_name,
					id: item.resource_id,
					link: item.link,
					date: item.date,
					icon: item.icon,
					sapp_type: item.sapp_type,
				}
			})
			favoriteList.value = {
				data,
				type: 'favorite',
			}
			store.commit('setCollectNumber', res.data?.length)
		}
	}

	return {
		getLoading,
		favoriteList,
		fetchFavoriteList,
	}
}
